﻿using EWS;
using Microsoft.Exchange.WebServices.Data;
using Quartz;
using System;
using System.Collections.Generic;
using UDataBaseWork;
using UEntity.LogEntity;
using XF.Meeting.DAL;

namespace QuartzJob
{
    public class GetMeetFromExchange : IJob
    {
        ExchangeToDbDal dal = new ExchangeToDbDal();
        public async System.Threading.Tasks.Task Execute(IJobExecutionContext context)
        {
            string strSqlConn = UCommonWork.Common.Commons.MD5DecodeString(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["SQLConnString"]));//
            CBaseDB m_db = m_db = new CBaseDB(strSqlConn);
            if (!m_db.ExcuteBySql(" truncate table dbo.SingleTemp ", out string strError))
            {
                ULogWork.CLogToTxtFile.RecordOperateLog(new EOperateLogEntity(" *********************单次Exchange会议临时表清空失败, error:" + strError));
            }
            var service = ExChangeHelper.getExChangeHelper();
            var meetList = new List<Appointment>();
            //  ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
            var meetss = service.GetRoomLists();
            foreach (var meetRooms in meetss)
            {
                var meetRoom = service.GetRooms(meetRooms);
                foreach (var room in meetRoom)
                {
                    try
                    {
                        var list = await ExChangeHelper.GetEmailListByRoom(room.Address);

                        //foreach (var meet in list)
                        //{

                        //      await  InsertOrUpdate(meet);
                        //}
                    }
                    catch (Exception e)
                    {

                        continue;
                    }


                }

            }
            CParameters parameters = new CParameters();
            if (!m_db.ExcuteByTransaction(parameters, "spCannelSingleTempSync", out strError))
            {
                ULogWork.CLogToTxtFile.RecordOperateLog(new EOperateLogEntity(" *********************同步取消Exchange单次会议失败, error:" + strError));

            }

        }
    }
}
